package merchant.space;


import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class BrowseCattegoriesActivity extends ListActivity {
	
	private BrowseCattegoriesActivity context;
	
	private static final int LOADING = 0;
	private static final int NOITEMS = 1;
	
	
	private AlertDialog alertdialog = null;
	private ProgressDialog progressdialog = null;
	
	private String[] cattegories;
	private String selectedCattegory;
	
	
	
	public void onCreate(Bundle savedInstanceState) {
		  super.onCreate(savedInstanceState);
		  
		  
		 
		  setContentView(R.layout.listing);
		  context=this;
		
		  cattegories=DataLink.getCattegories();
		  if (cattegories==null)
		  {
			 showDialog(LOADING);
			 new Thread(){ public void run() {
				
				  DataLink.loadCattegories();
				  cattegories = DataLink.getCattegories();
				  threadHandler.sendEmptyMessage(1);
			  }}.start();
		  }
		 
		 else refreshData();
		  
	
}
	
public void startBrowsingCollections()
{
	Intent browsecoll = new Intent(getParent(), BrowseCollectionsActivity.class);
	browsecoll.putExtra("cattegory", selectedCattegory);
	BrowseGroupActivity parentActivity = (BrowseGroupActivity) getParent();
	parentActivity.startChildActivity("BrowseCollectionsActivity", browsecoll);
}
	
	public void refreshData(){
		  setListAdapter(new ArrayAdapter<String>(this, R.layout.browse_cattegories,R.id.text, cattegories));
	}

	protected void onListItemClick(ListView l, View v, int position, long id) {
				super.onListItemClick(l, v, position, id);
				// Get the item that was clicked
				
				selectedCattegory = l.getItemAtPosition(position).toString();
				showDialog(LOADING);
				 
				new Thread(){ public void run() {
					Log.d("cat",selectedCattegory);
					DataLink.resetBrowsedCollections();
						  if (DataLink.getCollectionsByCattegory(selectedCattegory)!=null)
							  threadHandler.sendEmptyMessage(2);
						  else 
							  threadHandler.sendEmptyMessage(0);
					  }}.start();
	}
					  
		

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case LOADING:
			progressdialog = new ProgressDialog(getParent());
			progressdialog.setTitle("Merchant");
			progressdialog.setMessage("Loading data");
			progressdialog.setCancelable(false);
			return progressdialog;
		
		case NOITEMS:
			
			
			AlertDialog.Builder builder = new AlertDialog.Builder(getParent());
			builder.setMessage("Sorry, no collections to display")
			       .setCancelable(false)
			       .setTitle("Merchant")
			       .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
			             alertdialog.cancel();
			           }
			       });
			
			alertdialog = builder.create();
			return alertdialog;	
			
		default:
			return null;
		}
	}
	 
	
		private final Handler threadHandler = new Handler() {
			@Override
			public void handleMessage(Message arg0) {
				dismissDialog(LOADING);
				 switch(arg0.what)
				{
					
					case 1:
					{
						context.refreshData();
					} break;
					
					case 0:
					{
						context.showDialog(NOITEMS);
					} break;
					
					
					case 2:
					{
						context.startBrowsingCollections();
					} break;
					
					
					
					default:
					{
						 Log.d("aaah","FUCK");
					} break;
					
				
			}
		};
		};
	

		 public boolean onCreateOptionsMenu(Menu menu) {
			  super.onCreateOptionsMenu(menu);
			  menu.add(0,0,0,"Add Item");
			  Log.d("CREATING MENU","VIEW");
			
			  return true;
		    }
		    
		    public boolean onOptionsItemSelected(MenuItem item) {
		    	  super.onOptionsItemSelected(item);
		    	  switch (item.getItemId()) {
		    	  case 0:
		    		  
		    	//	 this.setContentView(R.layout.additem);
		    		  
		    	      break;
		    	 
		    	  
		    	  default:

		    	  break;
		    	  }
		    	  return false;
		    }
		
		}